package com.jf.cloud.service.sysmanager.support.web;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.taglib.TagService;
import com.jf.cloud.service.sysmanager.dao.ISysMenuService;
import com.jf.cloud.service.sysmanager.entity.SysMenu;
import com.jf.cloud.service.sysmanager.entity.SysStaff;
import com.jf.cloud.service.sysmanager.handler.TokenHandler;

/**
 * 当前系统 对标签的特殊处理
 * 
 * @author 卫江波
 * @2017年8月7日上午10:24:51
 */
@Component
public class TagServiceStrategy extends BaseController implements TagService{
    Logger logger = LoggerFactory.getLogger(TagServiceStrategy.class);
    
    @Resource
	private ISysMenuService menuService;
    @Resource
    private BaseDao baseDao;

    @Autowired
    private TokenHandler tokenHandler;
    
	/**
	 * 获取当前按钮的 描述
	 */
	public String getMenuDescByKey(String rightKey) {
		SysMenu menu =  menuService.findMenuByRightKey(rightKey);
		String desc = "";
		if(menu!=null && StringUtils.isNotBlank(menu.getMenuDesc())){
			 desc = menu.getMenuDesc();
		}
		return desc;
	}

	/**
	 * 检查当前按钮 +当前登录人是否有权限
	 * 如果返回 false 当前这个按钮或者html组件就不会展示
	 */
	public boolean checkPowerByUserAndRightKey(HttpServletRequest request, String rightKey) {
		try {
			String sql = " SELECT a.STAFF_TYPE '角色ID',b.ROLE_ID,b.MENU_ID,c.ROW_ID,c.URL,c.MENU_CODE "
					+ "   FROM sys_staff a,sys_role_vs_menu b,sys_menu c  WHERE a.STAFF_TYPE=b.ROLE_ID AND b.MENU_ID=c.ROW_ID "
					+ "   and a.ROW_ID=? and c.MENU_CODE=? ";
			
            SysStaff sysStaff = getLoginUser(request);
			
			if (sysStaff.getRowId() == 1) {
				return true;
			}
			java.util.List<Map<String, Object>> rs = baseDao.findMapList(sql, sysStaff.getRowId(), rightKey);
			if(rs!=null && rs.size()>0){
				return true;
			}
		} catch (Exception e) {
			
		}
		return  false;
	}
	/**
	 * 检查当前按钮 +当前登录人是否有权限
	 * 如果返回 false 当前这个按钮或者html组件就不会展示
	 */
	public boolean checkPowerByUserAndUrl(HttpServletRequest request, String url) {
		String sql = " SELECT a.STAFF_TYPE '角色ID',b.ROLE_ID,b.MENU_ID,c.ROW_ID,c.MENU_URL,c.MENU_CODE "
				+ "   FROM sys_staff a,sys_role_vs_menu b,sys_menu c  WHERE a.STAFF_TYPE=b.ROLE_ID AND b.MENU_ID=c.ROW_ID "
				+ "   and b.DEL_FLAG=0 and  a.ROW_ID=? and c.MENU_URL=? ";
		
		String token = request.getParameter("token");
        String sysStaffJson = tokenHandler.getSysStaffByToken(token);
        if (sysStaffJson == null || sysStaffJson.trim().length() == 0) {
            return false;
        }
        JSONObject sysStaff = JSONObject.fromObject(sysStaffJson);
		Long currUserId = sysStaff.getLong("rowId");
		
		if (currUserId == 1) {
			return true;
		}
		java.util.List<Map<String, Object>> rs = baseDao.findMapList(sql,currUserId,url); 
		if(rs!=null && rs.size()>0){
			return true;
		}
		return  false;
	}
	/**
	 * 注意： 开发前期 不要使用缓存
	 */
	public void initCache() {
		logger.info("  初始化缓存    ");
	}

}
